package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.domain.model.LoginUser;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.framework.web.service.TokenService;
import com.zhuoyue.system.domain.TblDeviceZY;
import com.zhuoyue.system.service.ITblDeviceZYService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * imageController
 *
 * @author hewenze
 * @date 2020-08-21
 */
@RestController
@Api(value = "卓悦设备管理接口", tags = "卓悦设备管理接口")
@RequestMapping("/system/deviceZY")
public class TblDeviceZYController extends BaseController {
    @Autowired
    private ITblDeviceZYService TblDeviceZYService;


    @Autowired
    private TokenService tokenService;

    /**
     * 查询image列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TblDeviceZY TblDeviceZY) {
        startPage();
        List<TblDeviceZY> list = TblDeviceZYService.selectTblDeviceZYList(TblDeviceZY);
        return getDataTable(list);
    }

    /**
     * 导出image列表
     */
    @Log(title = "image", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TblDeviceZY TblDeviceZY) {
        List<TblDeviceZY> list = TblDeviceZYService.selectTblDeviceZYList(TblDeviceZY);
        ExcelUtil<TblDeviceZY> util = new ExcelUtil<TblDeviceZY>(TblDeviceZY.class);
        return util.exportExcel(list, "image");
    }

    /**
     * 获取image详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(TblDeviceZYService.selectTblDeviceZYById(id));
    }

    /**
     * 新增image
     */
    @Log(title = "image", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TblDeviceZY TblDeviceZY, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        TblDeviceZY.setCreateBy(loginUser.getUsername());
        return toAjax(TblDeviceZYService.insertTblDeviceZY(TblDeviceZY));
    }

    /**
     * 修改image
     */
    @Log(title = "image", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TblDeviceZY TblDeviceZY) {
        return toAjax(TblDeviceZYService.updateTblDeviceZY(TblDeviceZY));
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(@RequestParam("deviceName") String deviceName,
                                 @RequestParam("deviceMac") String deviceMac,
                                 @RequestParam("categoryId") Long categoryId,
                                 @RequestParam("remark") String remark, HttpServletRequest request) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(request);


        TblDeviceZY TblDeviceZY = new TblDeviceZY();

        TblDeviceZY.setCategoryId(categoryId);
        TblDeviceZY.setDeviceName(deviceName);
        TblDeviceZY.setDeviceMac(deviceMac);

        TblDeviceZY.setCreateTime(new Date());
        TblDeviceZY.setCreateBy(loginUser.getUsername());

        this.TblDeviceZYService.insertTblDeviceZY(TblDeviceZY);
        return AjaxResult.success();
    }

    /**
     * 删除image
     */
    @Log(title = "image", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long[] ids) {

        return toAjax(TblDeviceZYService.deleteTblDeviceZYByIds(ids));
    }


}
















